package com.yl.myapplication.slice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yl.myapplication.ResourceTable;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyAbilitySlice2 extends AbilitySlice {

    Button btn;
    Text tv;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_my2);

        btn = (Button) findComponentById(ResourceTable.Id_btn);
        tv = (Text) findComponentById(ResourceTable.Id_tv);
        btn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getHttp();
            }
        });

    }

    private void getHttp() {

        String res = "{\n" +
                "    \"reason\": \"successed\",\n" +
                "    \"result\": {\n" +
                "        \"id\": \"3993\",\n" +
                "        \"yangli\": \"2021-03-01\",\n" +
                "        \"yinli\": \"辛丑(牛)年正月十八\",\n" +
                "        \"wuxing\": \"霹雳火 开执位\",\n" +
                "        \"chongsha\": \"冲马(壬午)煞南\",\n" +
                "        \"baiji\": \"戊不受田田主不祥 子不问卜自惹祸殃\",\n" +
                "        \"jishen\": \"母仓 时阳 生气 益後 青龙\",\n" +
                "        \"yi\": \"祭祀 沐浴 开光 塑绘 祈福 求嗣 订盟 纳采 冠笄 裁衣 嫁娶 动土 除服 成服 移柩 破土 启钻 出行 安碓　 放水 开市 立券 交易\",\n" +
                "        \"xiongshen\": \"灾煞 天火\",\n" +
                "        \"ji\": \"安葬 上梁 入宅 作灶\"\n" +
                "    },\n" +
                "    \"error_code\": 0\n" +
                "}";
        btn.setText("正在请求网络...");
        btn.setEnabled(false);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(new Date());
        //  String url = "https://zzrv5.github.io/test/zzr.html";
        String url = "http://v.juhe.cn/laohuangli/d?date=" + date + "&key=7cc85efc122e08f6ad8fa1f26877ddd8";
        ZZRHttp.get(url, new ZZRCallBack.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                //http访问出错了，此部分内容在主线程中工作;
                //可以更新UI等操作,请不要执行阻塞操作。
                tv.setText("网络访问失败!");
                btn.setText("点我访问网络");
                btn.setEnabled(true);
                GsonFormatData(res);
            }

            @Override
            public void onResponse(String response) {
                //http访问成功，此部分内容在主线程中工作;
                //可以更新UI等操作，但请不要执行阻塞操作。
                tv.setText(response);
                btn.setText("点我访问网络");
                btn.setEnabled(true);
                GsonFormatData(res);
            }
        });
    }

    private void GsonFormatData(String res) {

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LaoHuangLiBean laoHuangLiBean = objectMapper.readValue(res, LaoHuangLiBean.class);
            tv.setText(laoHuangLiBean.result.yinli);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            tv.setText("解析出错了!");
        }

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
